# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
"""
Dealing Stacks
"""

from ..packlib import info as cs
from .. import comparecards as cmp_cards
from .. import helpstrings as hs
from .. import hint
from . import pairing as PairingStack
from .. import pilelib as stack


class Camelot_Hint(hint.AbstractHint):

    def compute_hints(self):
        """
        compute_hints() -> None

        compute hints - main hint intelligence
        """
        rows = self._stacks['set']['rows']
        foundations = self._stacks['set']['foundations']
        talon = self._stacks['set']['talon']
        for from_stack in (p for p in rows if p.cards):
            if row.game.status:  # FIXME kludge lessen game imports
                if from_stack.BASECARD['rank'] == cs.TEN:
                    self.add_hint(5000, 1, from_stack, foundations[0])
                    return
                for to_stack in self._seq_builds_iter(rows, from_stack):
                    self.add_hint(5000, 1, from_stack, to_stack)
                    return
            if talon.cards and from_stack.cardsBuild(talon, talon.cards[-1:]):
                self.add_hint(5000, 1, talon, from_stack)
                return


class Camelot_RowStack(stack.GapFromTalonBuildMethod,
                       PairingStack.SumDropKingMethod, stack.ReserveStack):

    def _sequence(self, cards):
        """
        _sequence(cards:seq) -> bool

        Sequence is rank sum of 10.

        :param sequence cards: cards from sending pile
        :return: pile accepts cards?
        :rtype: boolean
        """
        return cmp_cards.sumOf(cards, cs.TEN)

    def cardsPlay(self, cards):
        """
        cardsPlay(cards:tup) -> bool

        Override playing. Only play when Game.status.

        :param sequence cards: cards from sending pile.
        :return: pile moves cards?
        :rtype: boolean

        note::
          No *capability* check performed.
        """
        return (self.game.status and
                super(Camelot_RowStack, self).cardsPlay(cards))


class GrandmammasPatience_RowStack(stack.NotFromRowStackBuildMethod,
                                   stack.BasicRowStack):
    pass


class PrincessPatience_RowStack(stack.GapFromWasteBuildMethod,
                                stack.SS_RowStack):

    def cardsBlock(self):
        """
        cardsBlock() -> bool

        Check if pile is blocked by seeing if piles of id have cards.

        :return: pile is blocked?
        :rtype: boolean
        """
        index = self.id
        col = index % 4
        pile_cards = lambda i: self.game.s['rows'][index + i].cards
        if index < 16:  # left side
            for i in range(col + 1, 4):
                if pile_cards(i):
                    return True
        else:  # right side
            for i in range(0, col):
                if pile_cards(i):
                    return True
        return False


class DoubleLine_RowStack(stack.FromWasteBuildMethod, stack.BasicRowStack):
    pass